#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int f[N], g[N];
int l[N], r[N];
int n;

int main()
{
    cin >> n;

    char num[20];
    for (int i = 0; i < n; i++)
    {
        scanf("%s", num);
        l[i] = num[0] - '0', r[i] = num[strlen(num) - 1] - '0';
    }

    int ans = 1;
    for (int i = 0; i < n; i++)
    {
        f[i] = 1;

        f[i] = g[l[i]] + 1;
        g[r[i]] = max(g[r[i]], f[i]);
        ans = max(ans, f[i]);
    }

    cout << n - ans;
    return 0;
}